package main

import (
	"blog/controller"
	"blog/dao"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"runtime"
)

func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())

	dao.Init()

	router := httprouter.New()
	//动态路由
	router.GET("/", controller.IndexHandle)
	/*搜索页*/
	router.GET("/search", controller.SearchHandle)
	router.GET("/search/tag/:tagName", controller.SearchHandle)
	router.GET("/search/types/:type", controller.SearchHandle)
	router.GET("/search/keyword/:keyWord", controller.SearchHandle)
	/*阅读*/
	router.GET("/read/:id", controller.ReadHandle)
	/*归档*/
	router.GET("/archive", controller.ArchiveHandle)
	/*获取文章评论*/
	router.GET("/comment/:id/:page", controller.CommentHandle)
	/*关于*/
	router.GET("/about", controller.AboutHandle)
	/*友情链接*/
	router.GET("/addLink", controller.AddLinkHandle)
	router.POST("/addLink", controller.ToAddLinkHandle)
	/**
	 *后台
	 */
	//登陆
	router.GET("/admin", controller.AdminIndexHandle)
	router.GET("/admin/user/login", controller.AdminUserLoginHandle)
	router.POST("/admin/user/login", controller.DoAdminUserLoginHandle)
	//退出登陆
	router.GET("/admin/user/logout", controller.AdminUserLogoutHandle)
	//后台
	router.GET("/admin/home", controller.AdminHomeHandle)
	router.GET("/admin/search", controller.AdminSearchHandle)

	router.GET("/admin/cache", controller.AdminCacheHandle)
	router.GET("/admin/cache/update", controller.AdminCacheUpdateHandle)

	router.GET("/admin/posts", controller.AdminPostsHandle)
	router.GET("/admin/posts/info/:id", controller.AdminPostsInfoHandle)
	router.GET("/admin/posts/list", controller.AdminPostsListHandle)
	router.POST("/admin/posts/add", controller.AdminPostsAddHandle)
	router.POST("/admin/posts/update", controller.AdminPostsUpdateHandle)

	router.GET("/admin/types", controller.AdminTypeHandle)
	router.GET("/admin/types/all", controller.AdminTypeAllHandle)
	router.POST("/admin/types/add", controller.AdminTypeAddHandle)
	router.POST("/admin/types/update", controller.AdminTypeUpdateHandle)

	router.GET("/admin/link", controller.AdminLinkHandle)
	router.GET("/admin/link/all", controller.AdminLinkAllHandle)

	router.GET("/admin/suggest", controller.AdminSuggestHandle)
	router.GET("/admin/admins", controller.AdminAdminsHandle)
	router.GET("/admin/version", controller.AdminVersionHandle)
	router.GET("/admin/backup", controller.AdminBackupHandle)
	router.GET("/admin/account", controller.AdminAccountHandle)
	router.GET("/admin/about", controller.AdminAboutHandle)
	router.GET("/admin/siteOpt", controller.AdminSiteOptHandle)
	router.GET("/admin/setting", controller.AdminSettingHandle)

	/**
	test
	*/
	router.GET("/test", controller.TestHandle)

	//静态资源
	for _, dirName := range []string{"img", "js", "css", "file", "editor"} {
		router.ServeFiles("/"+dirName+"/*filepath", http.Dir("static/"+dirName+"/"))
	}

	http.ListenAndServe(dao.HttpCfg.Listen+":"+dao.HttpCfg.Port, router)
}
